﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;

/// <summary>
/// Summary description for D_Sales
/// </summary>
/// 
namespace Bestlife.ERP.DataAccess
{
    public class D_Sales
    {
       public static Boolean result;
       public static string qty;
       public static string Use_ID;
        public D_Sales()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public static Bestlife.ERP.Entities.E_Sales sales(string rid)
        {
            SqlDataReader rdR;

            Bestlife.ERP.Entities.E_Sales invoice = new Entities.E_Sales();
            try
            {

                DateTime date = System.DateTime.Now;
                String connStr = ConfigurationManager.ConnectionStrings["BestlifeDBConnectionString"].ConnectionString;
                Bestlife.ERP.DataAccess.Connection.connect(connStr);//connection string pass to connection class


                String Ssql = "SELECT[R_ID],[D_id],[D_name],[Total_Price],[Toatal_PV],[Total_BV],[cashier_Name],[Rcpt_Date] FROM [Tbl_Receipt] where [R_ID]='"+rid+"' ";

                rdR = Connection.ReadTable(Ssql);

                if (rdR.Read())
                {
                    invoice.Cashiername = rdR["cashier_Name"].ToString();
                    invoice.D_id1 = rdR["D_id"].ToString();
                    invoice.D_name1 = rdR["D_name"].ToString();
                    invoice.R_id1 = rdR["R_ID"].ToString();
                    invoice.Totbv = rdR["Total_BV"].ToString();
                    invoice.Totprice = rdR["Total_Price"].ToString();
                    invoice.Totpv = rdR["Toatal_PV"].ToString();
                    invoice.Date = rdR["Rcpt_Date"].ToString();
                    
                }
                rdR.Close();
                
               

            }



            catch
            {
            }

            return invoice;

        }

        public static Boolean upd_recipt(string rid,string user)
        {
            
            try
            {
                DateTime date = System.DateTime.Now;
                string Cancel = "Cancel";
                string bc="BC";
                string riceptid = "BC"+rid;
                string userid = getuserid(user);
                String connStr = ConfigurationManager.ConnectionStrings["BestlifeDBConnectionString"].ConnectionString;
                Bestlife.ERP.DataAccess.Connection.connect(connStr);//connection string pass to connection class

                string sql = "UPDATE [Tbl_UserPurchase] SET [Status] ='" + Cancel + "',[Last_Modify] = '"+date+"',[Key_Timestamp] ='"+userid+"' WHERE [R_ID]='" + riceptid + "'";
                Boolean edit = Bestlife.ERP.DataAccess.Connection.AddEditDel(sql);
                if(edit)
                {
                    result=true;
                }
            }
            catch
            {
            }
            return result;

          
        }

        public static Boolean upd_stock(List<Bestlife.ERP.Entities.E_Sales> invoicesal,string user, int itemcount)
        {
            try
            {
                DateTime date = System.DateTime.Now;
                foreach (Bestlife.ERP.Entities.E_Sales sales in invoicesal)
                {
                    string cur_qty = stock_qty(sales.Proid, sales.Code);
                    decimal newqty = Convert.ToDecimal(cur_qty) + Convert.ToDecimal(sales.Qty);
                    String connStr = ConfigurationManager.ConnectionStrings["BestlifeDBConnectionString"].ConnectionString;
                    Bestlife.ERP.DataAccess.Connection.connect(connStr);//connection string pass to connection class

                    string sql = "UPDATE [Tbl_Issue_Stock] SET  [Qty_in_stock] = '" + newqty + "'  where [Pro_ID]='" + sales.Proid + "' and [Code]='" + sales.Code + "'";
                    Boolean edit = Bestlife.ERP.DataAccess.Connection.AddEditDel(sql);
                    if (edit)
                    {
                        result = true;
                    }
                }
            }
            catch
            {
            }
            return result;
        }

        public static Boolean upd_stockkandy(List<Bestlife.ERP.Entities.E_Sales> invoicesal, string user, int itemcount)
        {
            try
            {
                DateTime date = System.DateTime.Now;
                foreach (Bestlife.ERP.Entities.E_Sales sales in invoicesal)
                {
                    string cur_qty = stock_qty(sales.Proid, sales.Code);
                    decimal newqty = Convert.ToDecimal(cur_qty) + Convert.ToDecimal(sales.Qty);
                    String connStr = ConfigurationManager.ConnectionStrings["BestlifeDBConnectionString"].ConnectionString;
                    Bestlife.ERP.DataAccess.Connection.connect(connStr);//connection string pass to connection class

                    string sql = "UPDATE [Tbl_Issue_Stock] SET  [Kandy_Quantity] = '" + newqty + "'  where [Pro_ID]='" + sales.Proid + "' and [Code]='" + sales.Code + "'";
                    Boolean edit = Bestlife.ERP.DataAccess.Connection.AddEditDel(sql);
                    if (edit)
                    {
                        result = true;
                    }
                }
            }
            catch
            {
            }
            return result;
        }


        public static string stock_qty(string proid, string code)
        {
            SqlDataReader rdR;
            try
            {

                String connStr = ConfigurationManager.ConnectionStrings["BestlifeDBConnectionString"].ConnectionString;
                Bestlife.ERP.DataAccess.Connection.connect(connStr);//connection string pass to connection class


                String Ssql = "SELECT [Qty_in_stock] FROM [Tbl_Issue_Stock] where [Pro_ID]='" + proid + "' and [Code]='" + code + "'";

                rdR = Connection.ReadTable(Ssql);

                if (rdR.Read())
                {
                    qty = rdR["Qty_in_stock"].ToString();

                }
                rdR.Close();
            }
            catch
            {
            }
            return qty;
        }

        public static string stock_qtykandy(string proid, string code)
        {
            SqlDataReader rdR;
            try
            {

                String connStr = ConfigurationManager.ConnectionStrings["BestlifeDBConnectionString"].ConnectionString;
                Bestlife.ERP.DataAccess.Connection.connect(connStr);//connection string pass to connection class


                String Ssql = "SELECT [Kandy_Quantity] FROM [Tbl_Issue_Stock]  where [Pro_ID]='" + proid + "' and [Code]='" + code + "'";

                rdR = Connection.ReadTable(Ssql);

                if (rdR.Read())
                {
                    qty = rdR["Qty_in_stock"].ToString();

                }
                rdR.Close();
            }
            catch
            {
            }
            return qty;
        }

        public static Boolean del_receipt(string rid)
        {

            try
            {
                DateTime date = System.DateTime.Now;
                
                String connStr = ConfigurationManager.ConnectionStrings["BestlifeDBConnectionString"].ConnectionString;
                Bestlife.ERP.DataAccess.Connection.connect(connStr);//connection string pass to connection class

                string sql = "DELETE FROM [Tbl_Receipt]  WHERE [R_ID]='" + rid + "'";
                Boolean edit = Bestlife.ERP.DataAccess.Connection.AddEditDel(sql);
                if (edit)
                {
                    result = true;
                }
            }
            catch
            {
            }
            return result;


        }

        public static string getuserid(string username)
        {
            SqlDataReader rdR;
            try
            {
                DateTime date = System.DateTime.Now;
                String connStr = ConfigurationManager.ConnectionStrings["BestlifeDBConnectionString"].ConnectionString;
                Bestlife.ERP.DataAccess.Connection.connect(connStr);//connection string pass to connection class


                String Ssql = "SELECT [Use_ID] FROM [Tbl_Admin_User] where [Use_Name]='" + username + "'";

                rdR = Connection.ReadTable(Ssql);

                if (rdR.Read())
                {
                    Use_ID = rdR["Use_ID"].ToString();

                }
                rdR.Close();
            }
            catch
            {
            }

            return Use_ID;

        }

    }
}